<?php


namespace app\api\controller;

use app\admin\model\GoodsSpecificationKey;
use app\admin\model\GoodsSpecificationSku;
use app\admin\model\GoodsSpecificationVal;
use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\CollectionGoods;

use app\common\model\GoodsCategory;
use app\common\model\GoodsComment;
use think\cache\driver\Redis;

class Goods extends Api {

    protected $noNeedLogin = ['getGoodsList','getGoodsInfo','getCategoryList'];
    protected $noNeedRight = ['*'];

    /**
     * 获取分类列表
     */
    public function getCategoryList() {
        $id = input('id/d', 0);
        $model=new GoodsCategory();
        $list = $model->where('pid', '=', $id)->where('status','=','normal')->order('createtime desc')->select();

        // 轮播图
        $banner = [];
        if($id) {
            $banner[] = $model->where('id', '=', $id)->field(['coverimage'=>'image'])->find();
        }

        $this->success('请求成功', ['list'=>$list, 'banner'=>$banner]);
    }
    /**
     * 商品列表
     */
    public function getGoodsList() {
        $model = model('app\common\model\Goods');
        $category1 = input('category1');
        $category2 = input('category2');
        $category3 = input('category3');
        $goods_name = input('goods_name');
        $price_min = input('price_min');
        $price_max = input('price_max');
        $recommend = input('recommend',2);
        $type = input('type');//1综合2销量低3销量高4价格低5价格高
        $cid = input('cid');//分类id
        $where = [];
        $where['goods.status'] = '2';
        if ($category1) {
            $where['goods.category_id'] = $category1;
        }
        if ($category2) {
            $where['goods.category2_id'] = $category2;
        }
        if ($category3) {
            $where['goods.category3_id'] = $category3;
        }
        if($cid){
            $where['category3_id|category_id|category2_id'] = $cid;
        }
        if ($goods_name) {
            $where['goods.name'] = ['like', "%" . $goods_name . "%"];
        }
        if ($price_min){
            $where['goods.goods_price'] = ['>', $price_min];
        }
        if ($price_max){
            $where['goods.goods_price'] = ['<', $price_max];
        }
        if ($recommend==1){
            $where['goods.recommend'] = $recommend;
        }
//        if ($price_min && $price_max) {
//            $where['goods.goods_price'] = ['between', [$price_min, $price_max]];
//        }
        switch ($type) {
            case '1':
                $group_type = 'goods.createtime desc';
                break;
            case '2':
                $group_type = 'goods.sold_number';
                break;
            case '3':
                $group_type = 'goods.sold_number desc';
                break;
            case '4':
                $group_type = 'goods.goods_price ';
                break;
            case '5':
                $group_type = 'goods.goods_price desc';
                break;
            default:
                $group_type = 'goods.createtime desc';

        }
        $list = $model->with(['category2', 'category1', 'category3'])->where($where)->whereNull('goods.deletetime')->order($group_type)->paginate();

        foreach ($list as $row) {
            $row->visible(['id', 'category_id', 'category2_id', 'category3_id', 'brand_id', 'name', 'goods_price','goods_price_max', 'sold_number', 'thumbnail_image', 'goods_images', 'content', 'status', 'recommend', 'createtime', 'updatetime']);
            $row->visible(['category1']);
            $row->getRelation('category1')->visible(['name']);
            $row->visible(['category2']);
            $row->getRelation('category2')->visible(['name']);
            $row->visible(['category3']);
            $row->getRelation('category3')->visible(['name']);
        }
        $this->success('请求成功', $list);
    }

    /**
     * 获取商品详情
     */
    public function getGoodsInfo(){
        $redis=new Redis();
        $user_id=$this->auth->id;
        $goods_id=input('goods_id');
        $is_cate=input('is_cate', 0);
        $model = model('app\common\model\Goods');
        $info = $model->with(['category2', 'category1', 'category3'])->where('goods.id','=',$goods_id)->whereNull('goods.deletetime')->find();
        if (empty($info)){
            $this->error('当前商品不存在');
        }
        if($is_cate==1) {
            if ($info['status']==1){
                $this->error('当前商品已下架');
            }
        }
        $info->visible(['id', 'category_id', 'category2_id', 'category3_id', 'brand_id', 'name', 'goods_price', 'goods_price_max','sold_number', 'thumbnail_image', 'goods_images', 'content', 'status', 'recommend', 'createtime', 'updatetime']);
        $info->visible(['category1']);
        $info->getRelation('category1')->visible(['name']);
        $info->visible(['category2']);
        $info->getRelation('category2')->visible(['name']);
        $info->visible(['category3']);
        $info->getRelation('category3')->visible(['name']);
        //获取商品规格
        $key_model = new GoodsSpecificationKey();
        $val_model = new GoodsSpecificationVal();
        //商品组合
        $sku_model = new GoodsSpecificationSku();
        $key_list =toArray($key_model->where('goods_id','eq',$goods_id)->whereNull('deletetime')->select());
        foreach ($key_list as $k=>&$va){
            $va['attr_value']=toArray($val_model->where('goods_specification_key_id','eq',$va['id'])->whereNull('deletetime')->select());
        }
        $info=toArray($info);
        $info['goods_attr']=$key_list;
        $info['content']=imageUrl($info['content']);
        //判断当前商品是否收藏
        $Collection=new CollectionGoods();

        $is_check=$Collection->where('user_id','=',$user_id)->where('goods_id','=',$goods_id)->whereNull('deletetime')->count();
        $info['is_check']=$is_check>0?1:0;
        //$info['is_check']=0;

        $skuList=$sku_model->where('goods_id','=',$goods_id)->whereNull('deletetime')->select();
        $redis_key='szgoods_id'.$user_id.'_'.$goods_id;
        $redis->set($redis_key,$skuList);
        //获取商品库存
        $stock=$sku_model->where('goods_id','=',$goods_id)->whereNull('deletetime')->sum('stock');
        $info['stock']=$stock;
        //获取商品评论
        $goodsComment=new GoodsComment();
        $comment_list=$goodsComment->with(['user'])->where('goods_id','=',$goods_id)->whereNull('deletetime')->order('createtime desc')->limit(3)->select();
        foreach ($comment_list as $row) {
            $row->getRelation('user')->visible(['nickname','avatar']);
        }
        $info['goodsComm']=$comment_list;
        $info['countComm']=$goodsComment->where('goods_id','=',$goods_id)->whereNull('deletetime')->count();

        $this->success('请求成功', $info);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/11/10   9:43 上午
     * 获取商品评论列表
     */
    public function getGoodsCommentList(){
        $goods_id=input('goods_id');
        $goodsComment=new GoodsComment();
        $comment_list=$goodsComment->with(['user'])->where('goods_id','=',$goods_id)->whereNull('goods_comment.deletetime')->order('createtime desc')->paginate();
        foreach ($comment_list as $row) {
            $row->getRelation('user')->visible(['nickname','avatar']);
        }
        $this->success('请求成功', $comment_list);

    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/11/10   9:43 上午
     * 获取商品评论列表
     */
    public function getDiscussList(){
        $goods_id=input('goods_id');
        $goodsComment=new GoodsComment();
        $comment_list=$goodsComment->with(['user'])->where('goods_id','=',$goods_id)->whereNull('goods_comment.deletetime')->order('createtime desc')->paginate();
        foreach ($comment_list as $row) {
            $row->getRelation('user')->visible(['nickname','avatar']);
        }
        $this->success('请求成功', $comment_list);

    }
    /**
     * 商品切换规格
     */
    public function getGoodsAttr(){
        $goods_id=input('goods_id');
        $user_id=$this->auth->id;
        $attr_symbol_path=input('attr_symbol_path');

        $redis=new Redis();
        $redis_key='szgoods_id'.$user_id.'_'.$goods_id;

        $list=$redis->get($redis_key);
        if (!$list){
            $this->error('请重新打开商品详情');
        }

        $exp=explode('-',$attr_symbol_path);
        sort($exp);
        $new_exp=implode('-',$exp);
        $info=[];
        foreach ($list as $key=>$value){
            $tmpArr = explode('-', $value['attr_symbol_path']);
            sort($tmpArr);
            $tmpStr = implode('-', $tmpArr);
            if ($tmpStr==$new_exp){
                $info=$value;
                $info['img']=get_app_image_url($info['images']);
            }
        }
        $this->success('请求成功!', ['info'=>$info]);
    }

    /**
     * 添加收藏商品
     */
    public function addCollect(){
        $user_id=$this->auth->id;
        $goods_id=input('goods_id/d');
        $model=new CollectionGoods();
        $is_check=$model->where('goods_id','=',$goods_id)->where('user_id','=',$user_id)->whereNull('deletetime')->find();
        if ($is_check){
            $model->where('goods_id','=',$goods_id)->where('user_id','=',$user_id)->delete(true);
            $this->success('请求成功', null);
        }
        $argv['goods_id']=$goods_id;
        $argv['user_id']=$user_id;
        $model->create($argv);
        $this->success('请求成功', null);
    }
    /**
     * 删除收藏商品
     */
    public function delCollect(){
        $user_id=$this->auth->id;
        $goods_ids=input('goods_ids');
        $model=new CollectionGoods();
        $model->where('user_id','=',$user_id)->whereNull('deletetime')->whereIn('goods_id',$goods_ids)->delete(true);
        $this->success('请求成功', null);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/11/6   9:41 上午
     * 获取收藏商品列表
     */
    public function getCollectGoods(){
        $model=new CollectionGoods();
        $goodsModel=new \app\common\model\Goods();
        $user_id=$this->auth->id;
        $ids=$model->where('user_id','=',$user_id)->column('goods_id');
        $list=$goodsModel->whereIn('id',$ids)->paginate();
        $this->success('请求成功', $list);
    }
}